finp$="picture.jpg"
xed=320:yed=200

setdisplay(xed,yed,32,1):paper(8^8-1):ink(0):pen(0):cls

loadimage(finp$,1):pasteicon(0,0,1)
open finp$+".bin" for output as #1

setcaption("convert")

for y=0 to yed-1
    ro=0:go=0:bo=0:pf=0:zo=0
    for x=0 to xed-1
      ind=point(x,y)
      b=ind mod 256:ind=int(ind/256)
      g=ind mod 256:ind=int(ind/256)
      r=ind mod 256
      k=((b*11)+(r*30)+(g*59))/100
      b2=(int(b/4))*255/63
      g2=(int(g/4))*255/63
      r2=(int(r/4))*255/63
      k2=(int(k/4))*255/63
      bd=abs(b2-bo)
      rd=abs(r2-ro)
      gd=abs(g2-go)

      tg=0
      'if r2<>ro and g2<>go and b2<>bo then:bo=g2:ro=g2:go=g2:tg=1:end if
      if bd>=rd and bd>=gd and tg=0 then:bo=b2:pf=1:zo=bo:tg=1:end if
      if rd>=bd and rd>=gd and tg=0 then:ro=r2:pf=2:zo=ro:tg=1:end if
      if gd>=rd and gd>=bd and tg=0 then:go=g2:pf=3:zo=go:tg=1:end if

      ink(rgb(ro,go,bo)):dot(x,y)
      writebyte(1,bitwiseand(zo,0xFC)+bitwiseand(pf,0x03))
      next:next

close #1

grab (1,0,0,xed,yed):saveimage("_tmpr_.bmp",1):shell ("convert _tmpr_.bmp "+finp$+".png && rm _tmpr_.bmp")



